package actions;

import java.util.Map;

import javax.ejb.EJB;

import org.apache.struts2.ServletActionContext;

import beans.BookManager;
import beans.BookManagerBean;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import entities.Item;

public class RecallBooksAction extends ActionSupport {
	@EJB
	private BookManager bookManager = new BookManagerBean();
	
	private Item item = new Item();


	public String execute() {
		
		Map session = ActionContext.getContext().getSession();
		Integer id = (Integer) session.get("userId");
		if ( id == null ) {
			this.addActionError("User ID is null");
			return ERROR;
		}
		
		bookManager = new BookManagerBean();
		String copyid = ServletActionContext.getRequest().getParameter("bId");
		int val = Integer.parseInt(copyid.trim());

		Item item = bookManager.getCopy(val);
		if ( item.getCopyStatus() != Item.loaned ) {
			return "ErrorInRecall";
		}
		
		item.setCopyStatus(Item.recalled);
		if ( bookManager.updateCopy(item) ) return SUCCESS;
		
		this.addActionError("Book could not be recalled");
		return "ErrorInRecall";

	}

}
